package plateforme.controlleur;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import plateforme.connexion.ConnectionManager;

import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

/**
 * Servlet implementation class ControlleurChercherProjet
 */
public class ControlleurChercherProjet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
	private ServletConfig config;
	//Setting JSP page

	  String page="resultat_recherche_project.jsp";
	  String page1="chercher_projet.jsp";
	 
	  
	  
	  public void init(ServletConfig config) throws ServletException{

	 this.config=config;

	 }

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
		
		List<String>les_projets =new ArrayList<String>();
		String nom=request.getParameter("nom");
		String type=request.getParameter("type");
		
		
		 String name=null;
		 String typ=null;
		 String datedebut = null;
		 String datefin=null;
		 
		
		try {
			

			
			//System.out.println(role+specialite);
			
			
			ResultSet rs;
			Connection cn=null;
			 cn=ConnectionManager.getConnection();
			 String sql = "select * from project  where type='"+type+"'OR nom='"+nom+"'" ;
			//String sql = "select * from project  where type='"+type+"'" ;
			 Statement s = (Statement) cn.createStatement();

			  s.executeQuery (sql);

				
			  rs = (ResultSet) s.getResultSet();
			
			  while (rs.next ()){
				  
				  
				
				  //Add records into data list

				  les_projets.add(rs.getString(1));

				  les_projets.add(rs.getString(2));

				  les_projets.add(rs.getString(3));
				  
				  les_projets.add(rs.getString(4));
				  les_projets.add(rs.getString(5));
				
			  } 
			 
			 
			  Iterator <String>itr;
			  for (itr=les_projets.iterator();itr.hasNext(); )
			  {
				 name=itr.next(); 
				 typ=itr.next();
				 datedebut=itr.next();
				  datefin=itr.next();
				 
				  System.out.println(name+"+"+typ+"+"+datedebut+"+"+datefin); 
			  }
			  
			  
			
			  
			  rs.close ();

			  s.close ();

			 
			 
                  
			  //Disptching request
			 

			  
				// System.out.println(id+"+"+nom+"+"+prenom+"+"+role+""+fonction+""+login+"+"+password); 
				 //System.out.println(identifiant); 
				  
			  
		 
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		//Disptching request
        
  	  
		request.setAttribute("data",les_projets);

		  //Disptching request
		  RequestDispatcher dispatcher = request.getRequestDispatcher(page);

		 if (dispatcher != null){

		  dispatcher.forward(request, response);

		  

		  }
		 else{
			 
			 RequestDispatcher dispatcher1 = request.getRequestDispatcher(page1);

			  if (dispatcher1 != null){

			  dispatcher1.forward(request, response);

			  } 
			 
	}
	}
}
